package dms_gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import database_interaction.Database;
import database_interaction.Int;
import database_interaction.SelectQuery;
import database_interaction.UpdateQuery;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class dms_login {

	private JFrame frame;
	private JTextField textFieldUsername;
	private JLabel lblPassword;
	private JPasswordField pwdPassword;
	private JLabel lblStatus;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					dms_login window = new dms_login();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public dms_login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 237, 185);
		frame.setResizable(false);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblPleaseLogIn = new JLabel("Please Log In");
		lblPleaseLogIn.setBounds(10, 11, 211, 14);
		frame.getContentPane().add(lblPleaseLogIn);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(10, 36, 64, 14);
		frame.getContentPane().add(lblUsername);
		
		textFieldUsername = new JTextField();
		textFieldUsername.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyChar() == '\n')
		            login();
		        else
		        	lblStatus.setText("");
			}
		});
		textFieldUsername.setBounds(84, 33, 137, 20);
		frame.getContentPane().add(textFieldUsername);
		textFieldUsername.setColumns(10);
		
		lblPassword = new JLabel("Password");
		lblPassword.setBounds(10, 68, 64, 14);
		frame.getContentPane().add(lblPassword);
		
		pwdPassword = new JPasswordField();
		pwdPassword.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyChar() == '\n')
					login();
		        else
		        	lblStatus.setText("");
			}
		});
		pwdPassword.setBounds(84, 64, 137, 20);
		frame.getContentPane().add(pwdPassword);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				login();
			}
		});
		btnLogin.setBounds(132, 101, 89, 23);
		frame.getContentPane().add(btnLogin);
		
		lblStatus = new JLabel("");
		lblStatus.setBounds(10, 132, 211, 14);
		frame.getContentPane().add(lblStatus);
	}
	
	private void login(){
		try{
			Database db = new Database("jdbc:mysql://jd21/dms");
            db.connect("root", "");
            
            ResultSet result = 
            db.executeQuery(new SelectQuery("users",
                                            String.format("username = '%s' AND password = '%s'",
                                                          textFieldUsername.getText(),
                                                          new String(pwdPassword.getPassword())))
                            );
            if(result.first()){
                new MainForm(result.getInt("auth_level"),
                             result.getInt("id"),
                             db)
                            .setVisible(true);
                UpdateQuery loginUser = new UpdateQuery("users", "id = " + result.getInt("id"));
                loginUser.addParameter(new Int(1), "status");
                db.executeQuery(loginUser);
                frame.setVisible(false);
            }
            else{
                lblStatus.setText("Username or Password are incorrect.");
            }
			} catch (Exception ex){
				lblStatus.setText("Could not connect to server.");
			}
	}
}
